<?php
/* explain:		logs class
 * Project:     marsems
 * File:        Logs.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	Ivan  Ling
 * @ version 3.0
 *
 */
include_once("Common.php");

class Logs extends Common
{
	private $conHandle;		// connect sign
	private $strLanguage;
	
	private $iUserID;
	
	function Logs($userid)
	{
		parent::Common();
		$this->conHandle = $this->getConnection("system");
		$this->strLanguage = "en-US";
		
		$this->iUserID 	= $userid;
	}
	
	/* write error log
	 * 
	 * @ param  string userid
	 * @ param  strign errordate
	 * @ param  string errorname
	 * @ param  strign errorcode
	 * @ param  string desctype
	 * @ param  strign errormsg
	 * @ param  strign status 
	 * @ param  return boolean
	 */
	public function addError($userid=0,$errordate,$errorname,$errorcode,$desctype=0,$errormsg,$status='1')
	{
		if($desctype == 1)
			if(is_array($errormsg))
				$errormsg = serialize(serialize($errormsg));
			else
				return 0;
				
		$sql 	= "INSERT INTO error_log (userid,errordate,errorname,errorcode,desctype,errormsg,status) VALUES ($userid,'$errordate','$errorname',$errorcode,$desctype,'$errormsg','$status')";
		$rs		= mysql_query($sql,$this->conHandle);  
		$intErrorId = mysql_insert_id();
		if($rs)
			return $intErrorId;
		else
 			return 0;
	}

	/* get error info
	 * 
	 * @param  int id
	 * @param  return boolean
	 */
	public function getError($id)
	{
		$sql 	= "SELECT * FROM error_log WHERE id = $id";
		$rs		= mysql_query($sql, $this->conHandle);  
		if(mysql_num_rows($rs) > 0)
			return mysql_fetch_array($rs);
		else
 			return false;
	}
	
	/* write login log
	 * 
	 * @ param  string 		userid
	 * @ param  datetime 	logintime
	 * @ param  datetime 	logouttime
	 * @ param  strign 		fromip
	 * @ param  return 		boolean
	 */
	public function addLogin($userid, $logintime, $fromip)
	{
		$sql 	= "INSERT INTO login_log SET userid=$userid,logintime='$logintime', ip='$fromip'";
		$rs		= mysql_query($sql,$this->conHandle) or die ('error='.mysql_error());  
		$loginid = mysql_insert_id();
		if($rs)
			return $loginid;
		else
 			return false;
	}

	/* modify login log
	 *	 
	 * @ param  int 		id
	 * @ param  string 		userid
	 * @ param  datetime 	logintime
	 * @ param  datetime 	logouttime
	 * @ param  strign 		fromip
	 * @ param  return 		boolean
	 */
	public function escLogout($id, $logouttime, $ip)
	{
		$sql = "UPDATE login_log SET logouttime='$logouttime', ip='$ip' WHERE id=$id";
		$rs	 = mysql_query($sql,$this->conHandle); 
		if($rs)
			return true;
		else
			return false;
	}
	
	/* get login log
	 *
	 * @param  int id
	 * @param  return array
	 */
	public function arrGetLoginLog($id)
	{
		$sql = "SELECT * FROM login_log WHERE id = $id";
		$rs	 = mysql_query($sql,$this->conHandle); 
		if($rs)
			return mysql_fetch_array($rs);
		else
			return array();
	}

	/* login log list
	 *
	 * @ param  string 	where   	a?? where condition
	 * @ param  string 	page   		a?? curent page
	 * @ param  string 	discount   	a?? display data count
	 * @ param  string 	url   		a?? user url
	 * @ param  return 	array		a?? data array
	 */
	public function listLoginLog($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_VIEW))
			die ($this->getNotPopedomInfo());

		$this->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
		
		$arrback = array();
	
		$sql = "SELECT count(id) FROM login_log $where";
		$rs  = mysql_query($sql,$this->conHandle) or die(mysql_error());
		
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM login_log $where";
		else{
			$sql = "SELECT * FROM login_log $where LIMIT $this->firstcount, $discount";
		}

		$rs  = mysql_query($sql,$this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* login statistic
	 *	 
	 * @ param  int 	userid
	 * @ param  int 	intYear
	 * @ param  int 	intMonth
	 * @ param  int 	intDay
	 * @ param  return 	boolean
	 */
	public function addLoginStatistic($userid,$year,$month,$day)
	{
		$sql = "INSERT INTO login_statistic (userid,intYear,intMonth,intDay) VALUES ($userid,$year,$month,$day) ON DUPLICATE KEY UPDATE counts=VALUES(counts) + 1";
		$rs	 = mysql_query($sql,$this->conHandle) or die(mysql_error($this->conHandle).$sql); 
		
		if($rs)
			return true;
		else
			return false;
	}

	/* llist login statistic
	 *
	 * @ param  string 	where   	a?? where condition
	 * @ param  string 	page   		a?? curent page
	 * @ param  string 	discount   	a?? display data count
	 * @ param  string 	url   		a?? user url
	 * @ param  return 	array		a?? data array
	 */
	public function listLoginStatistic($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_VIEW))
			die ($this->getNotPopedomInfo());

		$this->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
		
		$arrback = array();
	
		$sql = "SELECT count(*) FROM login_statistic $where";
		$rs  = mysql_query($sql,$this->conHandle) or die(mysql_error());
		
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM login_statistic $where";
		else{
			$sql = "SELECT * FROM login_statistic $where LIMIT $this->firstcount, $discount";
		}

		$rs  = mysql_query($sql,$this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}

	/* get operaton info from id
	 *
	 * @param  int id
	 * @param  return array
	 */
	public function arrGetOperationFromID($id)
	{
		$sql = "SELECT * FROM operation WHERE id = $id";
		$rs	 = mysql_query($sql,$this->conHandle); 
		if($rs)
			return mysql_fetch_array($rs);
		else
			return array();
	}
	
	/* write operation log
	 * 
	 * @ param  int 		userid
	 * @ param  strign 		method		# operate method
	 * @ param  int			opid 		# operate id
	 * @ param  string 		comments	# comments
	 * @ param  return 		boolean
	 */
	public function addOperate($userid, $method, $opid, $comments = '')
	{
		if(empty($userid) || empty($opid))
			return false;
			
		$arrOPInfo = $this->arrGetOperationFromID($opid);
		if(count($arrOPInfo) > 0){
			$sql 	= "INSERT INTO operate_log set userid = '$userid', method = '$method', operation = '" . $arrOPInfo['name'] . "', comments = '$comments', addTime = now()";
			$rs		= mysql_query($sql,$this->conHandle) or die("Invalid query log-addOperate: " . mysql_error($this->conHandle));  
			if($rs)
				return true;
			else
	 			return false;
		}else
			return false;
	}

	/* llist operate log
	 *
	 * @ param  string 	where   	a?? where condition
	 * @ param  string 	page   		a?? curent page
	 * @ param  string 	discount   	a?? display data count
	 * @ param  string 	url   		a?? user url
	 * @ param  return 	array		a?? data array
	 */
	public function listOperateLog($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_VIEW))
			die ($this->getNotPopedomInfo());

		$this->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
		
		$arrback = array();
	
		$sql = "SELECT count(*) FROM operate_log $where";
		$rs  = mysql_query($sql,$this->conHandle) or die(mysql_error());
		
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM operate_log $where";
		else{
			$sql = "SELECT * FROM operate_log $where LIMIT $this->firstcount, $discount";
		}

		$rs  = mysql_query($sql,$this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* get temp login log
	 * 
	 * @param  string username
	 * @param  return boolean
	 */
	public function funGetLoginLogTmp($username)
	{
		$sql 	= "SELECT * FROM login_log_tmp WHERE username = '$username'";
		$rs		= mysql_query($sql, $this->conHandle) or die (mysql_error());  
		if(mysql_num_rows($rs) > 0)
			return mysql_fetch_array($rs);
		else
 			return false;
	}
	
	/* write temp login log
	 * 
	 * @ param  strign 	username
	 * @ param  string	ip
	 * @ param  return 	boolean
	 */
	public function funAddLoginLogTmp($username, $logintime, $ip)
	{
		$sql 	= "INSERT INTO login_log_tmp (username,logintime,logincount,ip) VALUES ('$username','$logintime',0,'$ip')";
		$rs		= mysql_query($sql, $this->conHandle) or die (mysql_error());
		if($rs)
			return true;
		else
 			return false;
	}

	/* modify temp login log
	 *	 
	 * @ param  string 	username
	 * @ param  int 	logincount
	 * @ param  return 	boolean
	 */
	public function funModLoginLogTmp($username, $logintime, $logincount)
	{
		$sql = "UPDATE login_log_tmp SET logincount=$logincount,logintime='$logintime' WHERE username='$username'";
		$rs	 = mysql_query($sql,$this->conHandle) or die("Invalid query log-funAddOperate: " . mysql_error());   
		if($rs)
			return true;
		else
			return false;
	}

}
?>
